package com.ruoyi.web.getHtml;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

/**
 * @className: GetHtml
 * @author: chenLin
 * @version: 1.0.0
 * @description:
 * @date: 24/5/31 031 11:01
 */

public class GetHtml {

    static WebDriver jiNanWebDriver;

    public static void main(String[] args) throws IOException, AWTException, InterruptedException {
        getHtmlByDriver();
//        ChromeOptions chromeOptions = new ChromeOptions();
//        chromeOptions.addArguments("--disable-blink-features=AutomationControlled");
//        System.getProperties().setProperty("webdriver.chrome.driver", "D:\\tmp\\chromedriver.exe");
//        chromeOptions.setExperimentalOption("useAutomationExtension", false);
//        chromeOptions.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
////        String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36";
////        chromeOptions.addArguments("user-agent=" + userAgent);
////        chromeOptions.addArguments("--headless");
//
//        jiNanWebDriver = new ChromeDriver(chromeOptions);
//        jiNanWebDriver.get("http://www.customs.gov.cn/customs/302249/302266/index.html");

        // 等待页面加载完成
        // 这里可以添加适当的等待时间，确保页面加载完成

        // 现在您可以继续使用 WebDriver 对象 driver 来执行各种操作，比如获取页面内容等

        // 关闭浏览器
    }
    /**
     * @param strURL
     * @return
     */
    public static void getHtmlByDriver() throws IOException, AWTException, InterruptedException {
        //设置谷歌驱动位置
        System.setProperty("webdriver.chrome.driver", "D:\\tmp\\chromedriver.exe");
        //谷歌浏览器启动端口
        int port = 9976 ;
        ////打开浏览器地址
        String url = "http://www.customs.gov.cn/customs/302249/302266/index.html";
        //设置谷歌启动参数
        //1 双击打开浏览器 chrome://version/
        //2 复制 命令行
        String str = "\"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe\" --flag-switches-begin --flag-switches-end --single-argument " + url;
        //设置谷歌浏览器目录
        System.setProperty("webdriver.chrome.bin", "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");
        //执行启动谷歌
        Process exec = Runtime.getRuntime().exec(str);
        InputStream inputStream = exec.getInputStream();

        // 将 InputStream 转换为字符串
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder output = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            output.append(line).append("\n");
        }

        // 打印转换后的字符串
        System.out.println(output.toString());

        // 关闭流
        reader.close();
        //创建 Robot 实例
        Robot robot = new Robot();
        //执行完一个事件后再执行下一个
        robot.setAutoWaitForIdle(true);
        ChromeOptions chromeOptions = new ChromeOptions();
        //指定启动端口 与打开的浏览器端口一致
        chromeOptions.setExperimentalOption("debuggerAddress", "localhost:" + port);
        jiNanWebDriver = new ChromeDriver(chromeOptions);
        jiNanWebDriver.get("http://www.customs.gov.cn/customs/302249/302266/index.html");
        //浏览器最大化
        jiNanWebDriver.manage().window().maximize();
        //设置全局读取超时时间
        jiNanWebDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);

//
//        ChromeOptions chromeOptions = new ChromeOptions();
//        chromeOptions.addArguments("--disable-blink-features=AutomationControlled");
//        System.getProperties().setProperty("webdriver.chrome.driver", "D:\\tmp\\chromedriver.exe");
//        chromeOptions.setExperimentalOption("useAutomationExtension", false);
//        chromeOptions.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
//        jiNanWebDriver = new ChromeDriver(chromeOptions);
//        jiNanWebDriver.get("http://www.customs.gov.cn/customs/302249/302266/index.html");
//        return jiNanWebDriver;
    }
}
